#测试详情页
import time
from hashlib import new
from telnetlib import EC

from selenium.webdriver.common.by import By
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.support.wait import WebDriverWait

from Common.Unity import ForumDriver
from selenium.webdriver.common.action_chains import ActionChains

class ForumDetail:
    url:""
    driver:""
    def __init__(self):
        self.url = "http://127.0.0.1:9580/index.html"
        self.driver = ForumDriver.driver
        self.driver.get(self.url)

    #登录状态下
    def DetailTestByLogin(self):
        #先点击
        self.driver.find_element(By.CSS_SELECTOR,"#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a > strong").click()
        time.sleep(3)
        #检查元素是否存在
        self.driver.find_element(By.CSS_SELECTOR,"#details_article_content_title")
        self.driver.find_element(By.CSS_SELECTOR,"#article_details_author_avatar")
        self.driver.find_element(By.CSS_SELECTOR,"#details_article_content")
        print("元素测试完成")

        # 测试点赞功能
        self.driver.find_element(By.CSS_SELECTOR, "#details_btn_like_count").click()
        print("点赞成功")

        #测试发私信

        self.driver.find_element(By.CSS_SELECTOR,"#btn_details_send_message").click()
        self.driver.find_element(By.CSS_SELECTOR,"#index_message_receive_content").send_keys("0000000000")
        self.driver.find_element(By.CSS_SELECTOR,"#btn_index_send_message").click()
        print("私信发送成功")
        time.sleep(1)
        self.driver.find_element(By.CSS_SELECTOR,"#index_message_receive_cancel").click()

        # 测试评论功能
        try:

            # 等待评论输入框可交互
            comment_input = WebDriverWait(self.driver, 10).until(
                EC.element_to_be_clickable((By.CSS_SELECTOR,
                                            "#article_details_reply > div.CodeMirror.cm-s-default.CodeMirror-wrap.CodeMirror-empty > div.CodeMirror-scroll > div.CodeMirror-sizer > div > div > div > div.CodeMirror-code > div > pre"))
            )
            # 使用 ActionChains 点击输入框
            actions = ActionChains(self.driver)
            actions.move_to_element(comment_input).click().perform()
            comment_input.send_keys("33333333")
            ForumDriver.getScreeShot()
            # 等待评论提交按钮可点击
            submit_button = WebDriverWait(self.driver, 10).until(
                EC.element_to_be_clickable((By.CSS_SELECTOR, "#details_btn_article_reply > span"))
            )
            submit_button.click()
            print("评论功能测试成功")
        except Exception as e:
            print(f"评论时出错: {e}")

        # # 测试评论功能
        # self.driver.find_element(By.CSS_SELECTOR,"#article_details_reply > div.CodeMirror.cm-s-default.CodeMirror-wrap.CodeMirror-empty > div.CodeMirror-scroll > div.CodeMirror-sizer > div > div > div > div.CodeMirror-code > div > pre").send_keys("33333333")
        # self.driver.find_element(By.CSS_SELECTOR,"#details_btn_article_reply > span").click()
        # print("评论功能测试成功")





